package org.red_cross_service.utils;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * token验证处理
 */
@Component
public class TokenUtil {

    //令牌密钥
    private static final String SECRET = "=Tutor!@#$%^&*()Back=";

    //令牌请求标识
    private static final String HEADER = "Authorization";

    //令牌有效期
    private static final int EXPIRE_TIME_MINUTE = 1800;

    @Autowired
    private RedisUtil redisUtil;

    public String createToken(String id) {
        String token = Jwts.builder()
                .setSubject(id) // 主题
                .setIssuedAt(new Date()) // 签发时间
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE_TIME_MINUTE)) // 过期时间
                .signWith(SignatureAlgorithm.HS512, SECRET) // 签名
                .compact();
        redisUtil.setCacheObject(token, id, EXPIRE_TIME_MINUTE, TimeUnit.SECONDS);
        return token;
    }

}
